import 'package:flutter/material.dart';
import 'package:my_app/base/widget/basic/base_lifecycle_page.dart';
import 'package:my_app/modules/tools/track/track_report.dart';

mixin PageTrack<T extends StatefulWidget> on BaseLifecycleState<T> {
  late DateTime _dateTime;

  /// 中文
  String get pageTitle;

  /// 英文
  String get pageName;

  String get goodId => '';

  String get sourceType => '';

  @override
  @mustCallSuper
  void onResumed() {
    _trackAppViewEvent();
  }

  @override
  @mustCallSuper
  void onPaused() {
    _trackAppExitEvent();
  }

  _trackAppViewEvent() {
    _dateTime = DateTime.now();
    TrackReport.trackEvent('enterPage', 'APP页面浏览', {
      'page_name': pageName,
      'page_title': pageTitle,
      'good_id': goodId,
      'source_type': sourceType,
    });
  }

  _trackAppExitEvent() {
    final currentDateTime = DateTime.now();
    final duration = currentDateTime.difference(_dateTime);
    TrackReport.trackEvent('leavePage', '离开页面', {
      'page_name': pageName,
      'page_title': pageTitle,
      'good_id': goodId,
      'source_type': sourceType,
      'event_duration': duration.inMilliseconds,
    });
  }
}
